package server.world;

import java.util.ArrayList;

import server.model.objects.Object;
import server.util.Misc;
import server.model.players.Client;
import server.Server;

/**
 * @author Sanity
 */

public class ObjectManager {

	public ArrayList<Object> objects = new ArrayList<Object>();
	private ArrayList<Object> toRemove = new ArrayList<Object>();
	public void process() {
		for (Object o : objects) {
			if (o.tick > 0)
				o.tick--;
			else {
				updateObject(o);
				toRemove.add(o);
			}		
		}
		for (Object o : toRemove) {
			if (isObelisk(o.newId)) {
				int index = getObeliskIndex(o.newId);
				if (activated[index]) {
					activated[index] = false;
					teleportObelisk(index);
				}
			}
			objects.remove(o);	
		}
		toRemove.clear();
	}
	
	public void removeObject(int x, int y) {
		for (int j = 0; j < Server.playerHandler.players.length; j++) {
			if (Server.playerHandler.players[j] != null) {
				Client c = (Client)Server.playerHandler.players[j];
				c.getPA().object(-1, x, y, 0, 10);			
			}	
		}	
	}
	
	public void updateObject(Object o) {
		for (int j = 0; j < Server.playerHandler.players.length; j++) {
			if (Server.playerHandler.players[j] != null) {
				Client c = (Client)Server.playerHandler.players[j];
				c.getPA().object(o.newId, o.objectX, o.objectY, o.face, o.type);			
			}	
		}	
	}
	
	public void placeObject(Object o) {
		for (int j = 0; j < Server.playerHandler.players.length; j++) {
			if (Server.playerHandler.players[j] != null) {
				Client c = (Client)Server.playerHandler.players[j];
				if (c.distanceToPoint(o.objectX, o.objectY) <= 60)
					c.getPA().object(o.objectId, o.objectX, o.objectY, o.face, o.type);
			}	
		}
	}
	
	public Object getObject(int x, int y, int height) {
		for (Object o : objects) {
			if (o.objectX == x && o.objectY == y && o.height == height)
				return o;
		}	
		return null;
	}
	
	public void loadObjects(Client c) {
		if (c == null)
			return;
		for (Object o : objects) {
			if (loadForPlayer(o,c))
				c.getPA().object(o.objectId, o.objectX, o.objectY, o.face, o.type);
		}
		loadCustomSpawns(c);
		if (c.distanceToPoint(2813, 3463) <= 60) {
			c.getFarming().updateHerbPatch();
		}
	}
	
	private int[][] customObjects = {{}};
	/*public void loadCustomSpawns(Client c) {
		c.getPA().checkObjectSpawn(2561, 3099, 3498, 0, 10);
		c.getPA().checkObjectSpawn(2560, 3099, 3495, 0, 10);
		c.getPA().checkObjectSpawn(2564, 3099, 3492, 0, 10);
		c.getPA().checkObjectSpawn(2562, 3099, 3489, 0, 10);//stalls
		c.getPA().checkObjectSpawn(1755, 3055, 9774, 0, 0);
		c.getPA().checkObjectSpawn(1596, 3008, 3850, 1, 0);
		c.getPA().checkObjectSpawn(1596, 3008, 3849, -1, 0);
		c.getPA().checkObjectSpawn(1596, 3040, 10307, -1, 0);
		c.getPA().checkObjectSpawn(1596, 3040, 10308, 1, 0);
		c.getPA().checkObjectSpawn(1596, 3022, 10311, -1, 0);
		c.getPA().checkObjectSpawn(1596, 3022, 10312, 1, 0);
		c.getPA().checkObjectSpawn(1596, 3044, 10341, -1, 0);
		c.getPA().checkObjectSpawn(1596, 3044, 10342, 1, 0);
		c.getPA().checkObjectSpawn(2213, 3047, 9779, 1, 10);
		c.getPA().checkObjectSpawn(2213, 3080, 9502, 1, 10);
		c.getPA().checkObjectSpawn(2475, 3233, 9312, 1, 10);
		c.getPA().checkObjectSpawn(4551, 2522, 3595, 1, 10);
		if (c.heightLevel == 0)
			c.getPA().checkObjectSpawn(2492, 2911, 3614, 1, 10);
		else
			c.getPA().checkObjectSpawn(-1, 2911, 3614, 1, 10);
	}*/
	public void loadCustomSpawns(Client c) {
		c.getPA().checkObjectSpawn(410, 3210, 3212, 1, 10); //alter of guthix
		//
		c.getPA().checkObjectSpawn(2561, 3225, 3222, 1, 10);
		c.getPA().checkObjectSpawn(2560, 3225, 3225, 1, 10);
		c.getPA().checkObjectSpawn(2564, 3225, 3214, 1, 10);
		c.getPA().checkObjectSpawn(2562, 3225, 3211, 1, 10);//stalls
		//
		c.getPA().checkObjectSpawn(4387, 3342, 3347, 1, 0);//skillling portal
		c.getPA().checkObjectSpawn(1596, 3008, 3850, 1, 0);//demongate kbd
		c.getPA().checkObjectSpawn(1596, 3008, 3849, -1, 0);//demon gate kbd
		c.getPA().checkObjectSpawn(1596, 3040, 10307, -1, 0);//gate in underground place, 51 wildy
		c.getPA().checkObjectSpawn(1596, 3040, 10308, 1, 0);//gate in underground place, 51 wildy
		c.getPA().checkObjectSpawn(1596, 3022, 10311, -1, 0);//gate in underground place, 51 wildy
		c.getPA().checkObjectSpawn(1596, 3022, 10312, 1, 0);//gate in underground place, 51 wildy
		c.getPA().checkObjectSpawn(1596, 3044, 10341, -1, 0);//gate in underground place, 51 wildy
		c.getPA().checkObjectSpawn(1596, 3044, 10342, 1, 0);//gate in underground place, 51 wildy
		c.getPA().checkObjectSpawn(6552, 3206, 3212, 2, 10);//ancient altar
		c.getPA().checkObjectSpawn(2213, 3047, 9779, 1, 10);//bank at falador mining spot
		c.getPA().checkObjectSpawn(2213, 3080, 9502, 1, 10);//bank at tutorial island mining spot
		c.getPA().checkObjectSpawn(13405, 2862, 3551, -1, 10);//agility portal home
		c.getPA().checkObjectSpawn(1391, 3229, 3210, -1, 10);//plant
		c.getPA().checkObjectSpawn(1392, 3230, 3209, -1, 10);//plant
		c.getPA().checkObjectSpawn(1393, 3230, 3226, -1, 10);//plant
		c.getPA().checkObjectSpawn(1391, 3229, 3227, -1, 10);//plant
		c.getPA().checkObjectSpawn(1392, 3229, 3228, -1, 10);//plant
		c.getPA().checkObjectSpawn(1393, 3229, 3229, -1, 10);//plant
		c.getPA().checkObjectSpawn(1391, 3230, 3232, -1, 10);//plant
		c.getPA().checkObjectSpawn(1392, 3231, 3233, -1, 10);//plant
		c.getPA().checkObjectSpawn(1393, 3232, 3234, -1, 10);//plant
		c.getPA().checkObjectSpawn(1391, 3222, 3235, -1, 10);//plant
		c.getPA().checkObjectSpawn(1392, 3230, 3261, -1, 10);//plant
		c.getPA().checkObjectSpawn(1393, 3230, 3263, -1, 10);//plant
		c.getPA().checkObjectSpawn(1392, 3233, 3242, -1, 10);//plant
		c.getPA().checkObjectSpawn(1393, 3233, 3243, -1, 10);//plant
		c.getPA().checkObjectSpawn(1393, 3232, 3235, -1, 10);//plant
		c.getPA().checkObjectSpawn(1393, 3230, 3231, -1, 10);//plant
		c.getPA().checkObjectSpawn(6951, 3217, 3218, -1, 0);
	    c.getPA().checkObjectSpawn(6951, 3238, 3210, -1, 0);
	    c.getPA().checkObjectSpawn(6951, 3217, 3219, -1, 0);
	    c.getPA().checkObjectSpawn(6951, 3213, 3221, -1, 0);
	    c.getPA().checkObjectSpawn(6951, 3213, 3222, -1, 0);
		c.getPA().checkObjectSpawn(7140, 2788, 4834, 0, 0);
				c.getPA().object(6951, 3209, 3219, 0, 10);		
				c.getPA().object(6951, 3209, 3224, 0, 10);	
				c.getPA().object(6951, 3208, 3223, 0, 10);	
c.getPA().object(6951, 3208, 3222, 0, 10);
c.getPA().object(6951, 3208, 3221, 0, 10);
c.getPA().object(6951, 3208, 3220, 0, 10);	
c.getPA().object(6951, 3209, 3223, 0, 10);	
c.getPA().object(6951, 3209, 3222, 0, 10);
c.getPA().object(6951, 3209, 3221, 0, 10);
c.getPA().object(6951, 3209, 3220, 0, 10);
c.getPA().object(6951, 3210, 3223, 0, 10);	
c.getPA().object(6951, 3210, 3222, 0, 10);
c.getPA().object(6951, 3210, 3221, 0, 10);
c.getPA().object(6951, 3210, 3220, 0, 10);
c.getPA().object(6951, 3205, 3226, 0, 10);
c.getPA().object(6951, 3205, 3225, 0, 10);
c.getPA().object(6951, 3205, 3224, 0, 10);
c.getPA().object(6951, 3205, 3223, 0, 10);
c.getPA().object(6951, 3205, 3222, 0, 10);
c.getPA().object(6951, 3205, 3221, 0, 10);
c.getPA().object(6951, 3205, 3220, 0, 10);
c.getPA().object(6951, 3205, 3219, 0, 10);
c.getPA().object(6951, 3205, 3218, 0, 10);
c.getPA().object(6951, 3212, 3226, 0, 10);
c.getPA().object(6951, 3212, 3225, 0, 10);
c.getPA().object(6951, 3212, 3224, 0, 10);
c.getPA().object(6951, 3212, 3223, 0, 10);
c.getPA().object(6951, 3212, 3222, 0, 10);
c.getPA().object(6951, 3212, 3221, 0, 10);
c.getPA().object(6951, 3212, 3220, 0, 10);
c.getPA().object(6951, 3212, 3219, 0, 10);
c.getPA().object(6951, 3212, 3218, 0, 10);
		//begin lumby spawns
		c.getPA().checkObjectSpawn(2213, 3212, 3218, 2, 10);
		c.getPA().checkObjectSpawn(2213, 3211, 3218, 2, 10);
		c.getPA().checkObjectSpawn(2213, 3210, 3218, 2, 10);
		c.getPA().checkObjectSpawn(2213, 3209, 3218, 2, 10);
		c.getPA().checkObjectSpawn(2213, 3208, 3218, 2, 10);
		c.getPA().checkObjectSpawn(2213, 3206, 3218, 2, 10);
		c.getPA().checkObjectSpawn(2213, 3205, 3218, 2, 10);
		//destroying walls, muahha!
		c.getPA().checkObjectSpawn(6951, 3213, 3215, 0, 10);
		c.getPA().checkObjectSpawn(6951, 3213, 3214, 0, 10);
		//inner room
		c.getPA().object(6951, 3205, 3217, 0, 10);
		c.getPA().object(6951, 3205, 3216, 0, 10);
		c.getPA().object(6951, 3205, 3215, 0, 10);
		c.getPA().object(6951, 3205, 3214, 0, 10);
		c.getPA().object(6951, 3205, 3213, 0, 10);
		c.getPA().object(6951, 3205, 3212, 0, 10);
		c.getPA().object(6951, 3206, 3215, 0, 10);
		c.getPA().object(6951, 3209, 3214, 0, 10);
		c.getPA().object(6951, 3208, 3214, 0, 10);
		c.getPA().object(6951, 3210, 3214, 0, 10);
		c.getPA().object(6951, 3212, 3212, 0, 10);
		c.getPA().object(6951, 3212, 3213, 0, 10);
		c.getPA().object(6951, 3212, 3214, 0, 10);
		c.getPA().object(6951, 3212, 3215, 0, 10);
		c.getPA().object(6951, 3212, 3216, 0, 10);
		c.getPA().object(6951, 3212, 3217, 0, 10);
		c.getPA().object(6951, 3211, 3217, 0, 10);
		c.getPA().object(6951, 3210, 3217, 0, 10);
		c.getPA().object(6951, 3209, 3216, 0, 10);
		c.getPA().object(6951, 3209, 3212, 0, 10);
		c.getPA().object(6951, 3207, 3217, 2, 10);
		c.getPA().object(6951, 3207, 3218, 2, 10);
		c.getPA().object(6951, 3211, 3212, 2, 10);
		c.getPA().object(6951, 3211, 3213, 2, 10);
		//test
		c.getPA().checkObjectSpawn(6951, 3207, 3217, -1, 0);
		c.getPA().checkObjectSpawn(6951, 3207, 3218, -1, 0);
		c.getPA().object(6951, 3209, 3216, -1, 0);
		c.getPA().checkObjectSpawn(6951, 3209, 3216, -1, 0);
		c.getPA().checkObjectSpawn(409, 3212, 3216, 1, 10);//prayer altar
		c.getPA().checkObjectSpawn(3884, 2659, 3646, 2, 10);
		c.getPA().checkObjectSpawn(3884, 2660, 3646, 2, 10);
		c.getPA().checkObjectSpawn(3884, 2661, 3646, 2, 10);
		c.getPA().checkObjectSpawn(3884, 2662, 3646, 2, 10);
		c.getPA().checkObjectSpawn(3884, 2663, 3646, 2, 10);
		c.getPA().checkObjectSpawn(3884, 2664, 3646, 2, 10);
		c.getPA().checkObjectSpawn(3884, 2665, 3646, 2, 10);
		c.getPA().checkObjectSpawn(5355, 3565, 3311, 2, 10);
		//
		c.getPA().checkObjectSpawn(1396, 3311, 3235, 2, 10);
		c.getPA().checkObjectSpawn(1396, 3311, 3234, 2, 10);
	//	c.getPA().checkObjectSpawn(3192, 3214, 3212, 3, 10); //score
		if (c.heightLevel == 2)
			c.getPA().checkObjectSpawn(13615, 2898, 5313, 1, 10);//Kreeeare
			c.getPA().checkObjectSpawn(13619, 2872, 5281, 2, 10);//zammy
			c.getPA().checkObjectSpawn(13620, 2854, 5324, 1, 10);//generelgrador
			c.getPA().checkObjectSpawn(13616, 2862, 5336, 2, 10);//saradomin
			//barrows
			c.getPA().checkObjectSpawn(10284, 3565,	3287, 2, 10);//saradomin
			
			c.getPA().checkObjectSpawn(2091, 2656,  3662, 2, 10);//mining
			c.getPA().checkObjectSpawn(2092, 2657,	3662, 2, 10);//mining
			c.getPA().checkObjectSpawn(2094, 2658,  3662, 2, 10);//mining
			c.getPA().checkObjectSpawn(2096, 2659,	3662, 2, 10);//mining
			c.getPA().checkObjectSpawn(2099, 2660,  3662, 2, 10);//mining
			c.getPA().checkObjectSpawn(2103, 2661,  3662, 2, 10);//mining
			c.getPA().checkObjectSpawn(2105, 2662,	3662, 2, 10);//mining
			c.getPA().checkObjectSpawn(3044, 2663,  3658, 2, 10);//furnace
			c.getPA().checkObjectSpawn(8389, 2661,  3653, 2, 10);//farm
			
			c.getPA().checkObjectSpawn(1276, 2651,	3654, 2, 10);//tree
			c.getPA().checkObjectSpawn(1281, 2648,  3654, 2, 10);//oak
			c.getPA().checkObjectSpawn(1308, 2650,  3650, 2, 10);//willow
			c.getPA().checkObjectSpawn(1307, 2648,	3649, 2, 10);//maple
			c.getPA().checkObjectSpawn(1309, 2653,  3648, 2, 10);//yew
			c.getPA().checkObjectSpawn(1306, 2657,  3648, 2, 10);//magic
			c.getPA().checkObjectSpawn(4387, 3222,	3223, 2, 10);//skilling portal
			c.getPA().checkObjectSpawn(359,	2858,	3426, 2, 10);//dishing
			c.getPA().checkObjectSpawn(2728, 2653,	3660, 2, 10);//cooking range
			c.getPA().checkObjectSpawn(2783, 2661,	3658, 2, 10);//anvil
			c.getPA().checkObjectSpawn(2213, 2655,	3663, 2, 10);//skilling bank		
			c.getPA().checkObjectSpawn(13289, 3214, 3213, 2, 10); //gambling
			c.getPA().checkObjectSpawn(2213, 3102, 9517, 3, 10); //jail bank
			c.getPA().checkObjectSpawn(999, 3258, 3230, 0, 10); //lumby pk gates
			c.getPA().checkObjectSpawn(999, 3260, 3231, 0, 10); //lumby pk gates
			c.getPA().checkObjectSpawn(999, 3262, 3232, 0, 10); //lumby pk gates
			c.getPA().checkObjectSpawn(999, 3264, 3233, 0, 10); //lumby pk gates
			c.getPA().checkObjectSpawn(998, 3266, 3233, 0, 10); //lumby pk gates
			c.getPA().checkObjectSpawn(1, 3267, 3228, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3267, 3227, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3267, 3226, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3267, 3229, 0, 10); //crates
			//anchor area
			c.getPA().checkObjectSpawn(1, 3479, 3076, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3480, 3077, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3470, 3090, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3469, 3090, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3468, 3090, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3467, 3090, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3470, 3101, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3469, 3102, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3469, 3103, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3469, 3105, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3474, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3475, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3476, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3477, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3478, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3479, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3480, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3481, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3482, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3483, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3484, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3485, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3486, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3487, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3488, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3489, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3490, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3491, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3492, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3106, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3105, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3104, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3103, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3102, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3101, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3100, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3099, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3098, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3493, 3097, 0, 10); //crates	
			c.getPA().checkObjectSpawn(1, 3471, 3094, 0, 10); //crates	
			c.getPA().checkObjectSpawn(1, 3470, 3094, 0, 10); //crates
			c.getPA().checkObjectSpawn(1, 3492, 3100, 0, 10); //crates			
	}
	
	public final int IN_USE_ID = 14825;
	public boolean isObelisk(int id) {
		for (int j = 0; j < obeliskIds.length; j++) {
			if (obeliskIds[j] == id)
				return true;
		}
		return false;
	}
	public int[] obeliskIds = {14829,14830,14827,14828,14826,14831};
	public int[][] obeliskCoords = {{3154,3618},{3225,3665},{3033,3730},{3104,3792},{2978,3864},{3305,3914}};
	public boolean[] activated = {false,false,false,false,false,false};
	
	public void startObelisk(int obeliskId) {
		int index = getObeliskIndex(obeliskId);
		if (index >= 0) {
			if (!activated[index]) {
				activated[index] = true;
				addObject(new Object(14825, obeliskCoords[index][0], obeliskCoords[index][1], 0, -1, 10, obeliskId,16));
				addObject(new Object(14825, obeliskCoords[index][0] + 4, obeliskCoords[index][1], 0, -1, 10, obeliskId,16));
				addObject(new Object(14825, obeliskCoords[index][0], obeliskCoords[index][1] + 4, 0, -1, 10, obeliskId,16));
				addObject(new Object(14825, obeliskCoords[index][0] + 4, obeliskCoords[index][1] + 4, 0, -1, 10, obeliskId,16));
			}
		}	
	}
	
	public int getObeliskIndex(int id) {
		for (int j = 0; j < obeliskIds.length; j++) {
			if (obeliskIds[j] == id)
				return j;
		}
		return -1;
	}
	
	public void teleportObelisk(int port) {
		int random = Misc.random(5);
		while (random == port) {
			random = Misc.random(5);
		}
		for (int j = 0; j < Server.playerHandler.players.length; j++) {
			if (Server.playerHandler.players[j] != null) {
				Client c = (Client)Server.playerHandler.players[j];
				int xOffset = c.absX - obeliskCoords[port][0];
				int yOffset = c.absY - obeliskCoords[port][1];
				if (c.goodDistance(c.getX(), c.getY(), obeliskCoords[port][0] + 2, obeliskCoords[port][1] + 2, 1)) {
					c.getPA().startTeleport2(obeliskCoords[random][0] + xOffset, obeliskCoords[random][1] + yOffset, 0);
				}
			}		
		}
	}
	
	public boolean loadForPlayer(Object o, Client c) {
		if (o == null || c == null)
			return false;
		return c.distanceToPoint(o.objectX, o.objectY) <= 60 && c.heightLevel == o.height;
	}
	
	public void addObject(Object o) {
		if (getObject(o.objectX, o.objectY, o.height) == null) {
			objects.add(o);
			placeObject(o);
		}	
	}




}